<?php
class Pages extends CI_Controller
{
	public function __construct ()
	{
		parent::__construct();
		$this->template->set_template('template/admin_template');
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
	}
	public function index ()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'Pages'));
		$data['pages'] = $this->general_model->select_rows('pages','*','title','asc','10');
		$this->template->content->view('admin/page/pages', $data);
		$this->template->publish();
	}
	public function addpage ()
	{
		$data['breadcrumb'] = set_crumbs(array('pages' => 'Pages', current_url() => 'Add Page'));
		$rulespages = $this->general_model->rules_pages;
		$this->form_validation->set_rules($rulespages);
		if ($this->form_validation->run() == TRUE)
		{
			$pagedata=array(
				'slug'=>$this->input->post('slug'),
				'title'=>$this->input->post('title'),
				/*'url'=>addslashes($this->input->post('url_path').$this->input->post('url')),*/
				'url'=>'/'.$this->input->post('url'),
				'content'=>addslashes($this->input->post('content')),
				'status'=>$this->input->post('status'),
				'template'=>$this->input->post('template'),
				'meta_title'=>$this->input->post('meta_title'),
				'meta_description'=>$this->input->post('meta_description'),
				'meta_keywords'=>$this->input->post('meta_keywords'),
				'date_added'=>date('Y-m-d H:i:s', strtotime($this->input->post('date_added')))
				);
			$pageid=$this->general_model->insert('pages',$pagedata);
			$this->session->set_flashdata('message', '<p class="success">Pages added Saved.</p>');
			if ($this->input->post('save_exit'))
			{
				redirect(ADMIN_PATH . "/pages");
			}
			else
			{
				redirect(ADMIN_PATH . "/pages/addpage");
			}
		}
		// Deteremine where to redirect user
		$this->template->content->view('admin/page/addpage', $data);
		$this->template->publish();
	}
	public function editpage()
	{
		$data['breadcrumb'] = set_crumbs(array('pages' => 'Pages', current_url() => 'Edit Page'));
		$pageid=$this->uri->segment(4);
		//echo $pageid;
		$rulespages = $this->general_model->rules_pages;
		$this->form_validation->set_rules($rulespages);
		if ($this->form_validation->run() == TRUE)
		{
			$pagedata=array(
				'slug'=>$this->input->post('slug'),
				'title'=>$this->input->post('title'),
				/*'url'=>addslashes($this->input->post('url_path').$this->input->post('url')),*/
				'url'=>'/'.$this->input->post('url'),
				'content'=>addslashes($this->input->post('content')),
				'status'=>$this->input->post('status'),
				'template'=>$this->input->post('template'),
				'meta_title'=>$this->input->post('meta_title'),
				'meta_description'=>$this->input->post('meta_description'),
				'meta_keywords'=>$this->input->post('meta_keywords'),
				'date_added'=>date('Y-m-d H:i:s', strtotime($this->input->post('date_added')))
				);
			$this->general_model->update('pages',$pagedata,"pageid = ".$pageid);
			$this->session->set_flashdata('message', '<p class="success">Pages Updated Successfully.</p>');
			if ($this->input->post('save_exit'))
			{
				redirect(ADMIN_PATH . "/pages");
			}
			else
			{
				redirect(ADMIN_PATH . "/pages/editpage/".$pageid);
			}
		}
		$data['pagedetails']=$this->general_model->select_val("pages","*","pageid = ".$pageid);
		$this->template->content->view('admin/page/editpage', $data);
		$this->template->publish();
	}
	public function deletepage()
	{
		if ($this->input->post('selected'))
        {
            $selected = $this->input->post('selected');
        }
        else
        {
            $selected = (array) $this->uri->segment(5);
        }
		$this->general_model->delete_rows('pages','pageid',$selected);
		$this->session->set_flashdata('message', '<p class="success">Page deleted Successfully.</p>');
		redirect(ADMIN_PATH . "/pages");
	}
}